import pageStyles from '@/common/pageLayout.less';
import { Table, message } from 'antd';
import { useMemo, useState } from 'react';
import { getHomeTableColumns } from '../mconfig';
import { getKeyRouter } from '@/utils';
import Cls from 'classnames';
import { commonPageSizeOptions, showTotalInfo } from '@/common/constVariable';
import { history } from 'umi';
// import { getMcontractByIDService } from '../services';
import { DocumentationHomeAPI } from '../common/type';

export const DocumentationTableBody: React.FC<DocumentationHomeAPI.IDocumentationTableBody> = ({
  curRes,
  tabPaneKey,
  listTableData,
  pageSizeObj,
  getTaskCheckData,
}) => {
  /** 保存变更合同ID */
  const [modifyInfoDetail, setModifyInfoDetail] = useState<Record<string, any>>({});

  /** 详情回调 */
  const detailsCallback = (res: any) => {
    if (res && res?.msgFlag) {
      history.push({
        pathname: `/contractController/getMcontractByID/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          id: modifyInfoDetail?.clickType ? modifyInfoDetail?.pID : modifyInfoDetail?.busi_id,
          pageInfo: {
            pID: modifyInfoDetail?.busi_pID ? modifyInfoDetail?.busi_pID : null,
            id: modifyInfoDetail?.busi_id ? modifyInfoDetail?.busi_id : null,
          },
          pIDDetailInfo: {
            ...res?.obj,
          },
        },
      });
      setModifyInfoDetail({});
    } else {
      message.error(res?.msg);
    }
  };

  /** 获取详情 */
  //   const { getMcontractByIDRun } = getMcontractByIDService(detailsCallback);

  /** 进入详情办理页面方法 */
  const onViewHand = (resObj: Record<string, any>) => {
    if (resObj?.attributes.busi_changeWay == 0 || resObj?.attributes.busi_changeWay == 1) {
      setModifyInfoDetail(resObj?.attributes);
      //   getMcontractByIDRun({
      //     id: resObj?.attributes.busi_pID,
      //   });
    } else {
      history.push({
        pathname: `/contractController/getMcontractByID/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          id: resObj?.attributes.busi_id,
          pageInfo: {
            pID: resObj?.attributes?.busi_pID ? resObj?.attributes?.busi_pID : null,
            id: resObj?.attributes?.busi_id ? resObj?.attributes?.busi_id : null,
          },
          pIDDetailInfo: {
            ...resObj?.attributes,
            id: resObj?.attributes?.busi_id ? resObj?.attributes?.busi_id : null,
            pID: resObj?.attributes?.busi_pID ? resObj?.attributes?.busi_pID : null,
            contractCode: resObj?.attributes?.busi_contractCode,
          },
        },
      });
    }
  };

  /** 点击方法 */
  const onOptClick: DocumentationHomeAPI.IOperationClick = {
    onView: (resObj) => {
      //   onViewHand(resObj);
    },
    onHand: (resObj) => {
      //   onViewHand(resObj);
    },
  };

  /** 返回表格配置 */
  const columnsMemo = useMemo(() => {
    return getHomeTableColumns(curRes, tabPaneKey == '1' ? true : false, onOptClick);
  }, [tabPaneKey, curRes]);

  // 分页页数 和数据条数改变
  const handlePaginationChange = (page: number, pageSize: number) => {
    let params = {
      ...pageSizeObj,
      current: page,
      pageSize: pageSize,
      taskType: tabPaneKey == '1' ? false : true,
    };
    getTaskCheckData(params);
  };
  /** 点击跳转 */
  const onHandleSkip = (resObj: any) => {
    history.push({
      pathname: `/purchaseSourceController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
      state: {
        pageType: 'detail',
        id: resObj?.attributes.busi_id,
        pageInfo: {
          id: resObj?.attributes?.busi_id ? resObj?.attributes?.busi_id : null,
        },
        pIDDetailInfo: {
          ...resObj?.attributes,
          id: resObj?.attributes?.busi_id ? resObj?.attributes?.busi_id : null,
        },
      },
    });
  };
  return (
    <div className="commonTabsBodyDom" style={{ padding: '5px 0px' }}>
      <div className={Cls(pageStyles['listTable-zIndexStyle'], pageStyles['listTable-notBorder'])}>
        <Table
          size="small"
          bordered={false}
          columns={columnsMemo}
          scroll={{ x: '100vh', y: 'true' }}
          dataSource={listTableData?.rows}
          rowKey={(res: any) => res?.id || getKeyRouter()}
          onRow={(record) => {
            return {
              onClick: () => {
                onHandleSkip(record);
              },
            };
          }}
          pagination={{
            showQuickJumper: true,
            showSizeChanger: true,
            pageSizeOptions: commonPageSizeOptions,
            showTotal: showTotalInfo,
            onChange: handlePaginationChange,
            pageSize: pageSizeObj?.pageSize,
            current: pageSizeObj?.current,
            total: pageSizeObj?.total,
          }}
        />
      </div>
    </div>
  );
};
